import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'

const server = axios.create({
    // baseURL: import.meta.env.VITE_API_URL,
    // baseURL: import.meta.env.VITE_MOCK_API,
    baseURL: '',
    timeout: 50000
})

server.interceptors.request.use(
    (config) => {
        // 添加hearders字段
        config.headers['X-Token'] = 'my token'
        return config
    },
    (err) => {
        return Promise.reject(err)
    }
)

server.interceptors.response.use(
    (response) => {
        const res = response.data
        if (res.status !== '0') {
            ElMessage.error({
                message: res.message,
                duration: 5 * 1000
            })
            // 继续判断状态码
        } else {
            return res
        }
    },
    (err) => {
        ElMessage({
            message: err.message,
            type: 'error',
            duration: 5 * 1000
        })
        return Promise.reject(err)
    }
)

export default server
